package com.rush.manageepac.service.impl;

import com.rush.manageepac.dao.CommunityMaterialDao;
import com.rush.manageepac.dao.CommunityPeopleDao;
import com.rush.manageepac.entity.CommunityMaterial;
import com.rush.manageepac.entity.CommunityPeople;
import com.rush.manageepac.service.intf.CommunityMaterialService;
import com.rush.manageepac.service.intf.CommunityPeopleService;
import com.rush.manageepac.util.StringUtil;
import com.rush.manageepac.vo.PageBean;
import com.rush.manageepac.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 于zH
 * @data 2022/8/29 9:04
 */
@Service
public class CommunityMaterialServiceImpl implements CommunityMaterialService {
    @Autowired
    private CommunityMaterialDao communityMaterialDao;

    @Override
    public R save(CommunityMaterial communityMaterial) {
        if (communityMaterialDao.save(communityMaterial) > 0) {
            return R.ok();
        } else {
            return R.fail("添加失败");
        }
    }

    @Override
    public R update(CommunityMaterial communityMaterial) {
        if (communityMaterialDao.update(communityMaterial) > 0) {
            return R.ok();
        } else {
            return R.fail("修改失败");
        }
    }

    @Override
    public R delete(int id) {
        if (communityMaterialDao.delete(id) > 0) {
            return R.ok();
        } else {
            return R.fail("删除失败");
        }
    }

    @Override
    public PageBean selectPage(String name, int page, int limit) {
        if(StringUtil.isNotEmpty(name)){
            name="%"+name+"%";
        }else{
            name = null;
        }
        long count = communityMaterialDao.selectCount(name);
        int start = (page - 1)*limit;
        return PageBean.ok(count,communityMaterialDao.selectPage(name, start, limit));
    }

    @Override
    public PageBean selectPage2(String type, int page, int limit) {
        if(StringUtil.isNotEmpty(type)){
            type="%"+type+"%";
        }else{
            type = null;
        }
        long count = communityMaterialDao.selectCount(type);
        int start = (page - 1)*limit;
        return PageBean.ok(count,communityMaterialDao.selectPage(type, start, limit));
    }
}
